Skip to main content

rm command

rm - remove files or directories

Changes the shell working directory.

Usage: rm [OPTION]... [FILE]...

  • OPTION: Flags which enhances the rm abilities.
  • FILE: The file or directory you want to remove i.e., delete permanently.

It is very important to be extra cautious while using rm command because this will delete files and directories permanently.

It would be safer to use -i flag always or in conjunction with the other flags, which will prompt to confirm before deleting files or directories.

Examples

  • Deleting a single file

    $ rm document.txt
    • Deletes document.txt from the current directory.
    • No confirmation is asked, and the file is permanently removed.

    Safer Deletion would be to use -i (interactive) option to confirm each deletion:

    $ rm -i document.txt
    rm: remove regular empty file 'document.txt'?
    • Prompts you with: rm: remove regular file 'document.txt'? (Type y to confirm, n to skip.)
  • Deleting multiple files

    $ rm file1.txt file2.txt
    • Deletes both file1.txt and file2.txt.
    • To Delete multiple files with common name pattern we can use wildcards
    $ rm *.txt
    • Deletes all .txt files in the current directory.
  • Deleting directories

    To delete a directory and all of its contents inside, we have to use the -r (recursive) option.

    $ rm /home/user/old_project
    rm: cannot remove 'project/': Is a directory
    $ rm -r /home/user/old_project
    • Without -r, rm will fail and complain about directories.
    • Using -r deletes the old_project directory and everything inside it.

    Its always advised to use -ri flag while deleting directories to get confirmation prompt.

  • Forcing deletion

    Use -f (force) to delete without prompts or errors, even if files don’t exist.

    $ rm -f non_existent_file.txt
    • No error is shown, even if the file doesnot exist there.
    • Most dangerous and powerful combo would be -rf which deletes directories silently.
    $ rm -rf /home/user/temp/
    • Deletes temp and all its contents including subdirectories and files without asking. Use with extreme caution!
  • Verbose output

    Want to see what’s happening while the rm operation is running? Use -v (verbose) to display each action and what’s being deleted.

    $ rm -v file1.txt
    removed 'file1.txt'

    With directories

    $ rm -rv proj
    removed directory 'proj/bin'
    removed directory 'proj/src'
    removed directory 'proj/docs'
    removed directory 'proj'
    • Prints each file and subdirectory as they getting removed.
$ rm --help
Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).

-f, --force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or
when removing recursively; less intrusive than -i,
while still giving protection against most mistakes
--interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
--one-file-system when removing a hierarchy recursively, skip any
directory that is on a file system different from
that of the corresponding command line argument
--no-preserve-root do not treat '/' specially
--preserve-root[=all] do not remove '/' (default);
with 'all', reject any command line argument
on a separate device from its parent
-r, -R, --recursive remove directories and their contents recursively
-d, --dir remove empty directories
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit

By default, rm does not remove directories. Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.

To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
rm -- -foo

rm ./-foo

Note that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time. For greater
assurance that the contents are truly unrecoverable, consider using shred.